//
//  LMySettingGeneralViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LMySettingGeneralViewController.h"
#import "LGeneralModel.h"
#import "LMySettingGeneralTableViewCell.h"
#import "LCustomAlertView.h"
#import "JCAlertView.h"
#import "LPClearTheCache.h"
@interface LMySettingGeneralViewController ()<UITableViewDelegate,UITableViewDataSource,LCustomAlertDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *data;
/**    */
@property (strong, nonatomic) JCAlertView  *jcAlertView;

@end

@implementation LMySettingGeneralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"通用";
    [self getDataSource];
    [self layoutUI];
}

#pragma mark - 数据
- (void)getDataSource{
    
    NSString *huancun = [NSString stringWithFormat:@"%.1f",[LPClearTheCache getFilePath]];
    NSArray *array = @[
  @{@"image": @"mine_set_qingchuhuancun", @"title": @"清除缓存", @"detail": huancun},
   @{@"image": @"mine_set_autoplay", @"title": @"是否自动播放", @"detail": @""}
  ];
    for (NSDictionary *dic in array) {
        [self.data addObject:[LGeneralModel yy_modelWithDictionary:dic]];
    }
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 12 * [UIScreen zj_screenHScale])];
    head.backgroundColor = [UIColor themeGrayColor];
    self.tableView.tableHeaderView = head;
}


#pragma mark - 私有方法

#pragma mark - 监听事件

#pragma mark - LCustomAlertDelegate
- (void)lCustomAlertWithView:(LCustomAlertView *)view index:(NSInteger)index{
    if (index == 1) {
        /**  确认  */
        [LPClearTheCache clearTheCache];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD showMessage:@"清除成功" inView:self.view];
            LGeneralModel *model = self.data[0];
            model.detail = @"0.0";
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index, nil] withRowAnimation:UITableViewRowAnimationNone];
        });
    }
    [self.jcAlertView dismissWithCompletion:nil];
}


/**  UITableViewDataSource,UITableViewDelegate  */
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellid = @"cellId";
    LMySettingGeneralTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[LMySettingGeneralTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.model = self.data[indexPath.row];
    return cell;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LGeneralModel *model = self.data[indexPath.row];
    if ([model.title isEqualToString:@"清除缓存"]) {
        LCustomAlertView *alert = [[LCustomAlertView alloc] initWithFrame:self.view.bounds alertMessage:@"是否清除缓存" titlesArray:@[@"取消",@"确认"]];
        alert.delegate = self;
        self.jcAlertView = [[JCAlertView alloc]initWithCustomView:alert dismissWhenTouchedBackground:YES];
        [self.jcAlertView show];

    }
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
    }
    return _tableView;
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;

}
@end
